package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.pinda.exception.BizException;
import com.itheima.wms.dto.OutboundListBatchDTO;
import com.itheima.wms.dto.OutboundListDTO;
import com.itheima.wms.entity.OutboundListEntity;
import com.itheima.wms.service.OutboundListService;
import com.itheima.wms.vo.OutboundListDetailVO;
import com.itheima.wms.vo.OutboundListSumVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 出库清单
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/outboundList")
@Api(value = "OutboundList", tags = "出库清单")
public class OutboundListController extends BaseController {

    @Autowired
    private OutboundListService outboundListService;

    /**
     * 分页查询出库清单
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询出库清单", notes = "分页查询出库清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "stockId", value = "库存", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "outboundNum", value = "发货数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pickingNum", value = "实拣数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "differenceNum", value = "数量差异", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "masterId", value = "上级id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<OutboundListEntity>> page(@RequestParam Map data) {
        Page<OutboundListEntity> page = getPage();
        IPage<OutboundListEntity> outboundListPage = outboundListService.page(page, data);
        page.setRecords(outboundListPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "分页查询出库清单明细", notes = "分页查询出库清单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "masterId", value = "主记录id", dataType = "long", paramType = "query"),
    })
    @GetMapping("/pageDetail")
    public R<IPage<OutboundListDetailVO>> pageDetail(@RequestParam Map data) {
        Page<OutboundListDetailVO> page = getPage();
        IPage<OutboundListDetailVO> outboundListDetailVOIPage = outboundListService.pageDetail(page, data);
        page.setRecords(outboundListDetailVOIPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询出库清单关联的库位id集合", notes = "查询出库清单关联的库位id集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "masterId", value = "主记录id", dataType = "long", paramType = "query"),
    })
    @GetMapping("/stockIds")
    public R<List<Long>> stockIds(@RequestParam Map data) {
        List<Long> ids = outboundListService.stockIds(data);
        return success(ids);
    }

    @ApiOperation(value = "查询全部出库清单", notes = "查询全部出库清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "stockId", value = "库存", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "outboundNum", value = "发货数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pickingNum", value = "实拣数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "differenceNum", value = "数量差异", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "masterId", value = "上级id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<OutboundListEntity>> list(@RequestParam Map data) {
        List<OutboundListEntity> outboundListArray = outboundListService.list(data);
        return success(outboundListArray);
    }

    /**
     * 查询出库清单
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询出库清单", notes = "查询出库清单")
    @GetMapping("/{id}")
    public R<OutboundListEntity> get(@PathVariable Long id) {
        return success(outboundListService.getById(id));
    }

    /**
     * 查询合计信息
     *
     * @param masterId 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询合计信息", notes = "查询合计信息")
    @GetMapping("/sum/{masterId}")
    public R<OutboundListSumVO> getSumByMasterId(@PathVariable Long masterId) {
        return success(outboundListService.getSumByMasterId(masterId));
    }

    /**
     * 新增出库清单
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增出库清单", notes = "新增出库清单不为空的字段")
    @PostMapping
    public R<OutboundListEntity> save(@RequestBody @Validated(SuperEntity.Save.class) OutboundListDTO data) {
        outboundListService.save(data);
        return success(data);
    }

    /**
     * 批量新增出库清单
     *
     * @param outboundListBatchDTO 批量新增模型
     * @return 新增结果
     */
    @ApiOperation(value = "批量新增出库清单", notes = "批量新增出库清单")
    @PostMapping("batch")
    public R<Boolean> saveBatch(@RequestBody @Validated OutboundListBatchDTO outboundListBatchDTO) {
        if (CollectionUtils.isEmpty(outboundListBatchDTO.getStockIds())) {
            throw new BizException("参数为空");
        }
        if (null == outboundListBatchDTO.getMasterId()) {
            throw new BizException("参数为空");
        }
        return success(outboundListService.saveBatchByStockIds(outboundListBatchDTO));
    }

    /**
     * 修改出库清单
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改出库清单", notes = "修改出库清单不为空的字段")
    @PutMapping
    public R<OutboundListEntity> update(@RequestBody @Validated(SuperEntity.Update.class) OutboundListDTO data) {
        outboundListService.updateById(data);
        return success(data);
    }

    /**
     * 删除出库清单
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除出库清单", notes = "根据id物理删除出库清单")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        outboundListService.removeByIds(ids);
        return success();
    }

    /**
     * 根据masterId删除清单
     *
     * @param masterId 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "根据masterId删除清单", notes = "根据masterId删除清单")
    @DeleteMapping("masterId")
    public R<Boolean> delete(@RequestParam(value = "masterId", required = true) Long masterId) {
        LambdaQueryWrapper<OutboundListEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OutboundListEntity::getMasterId, masterId);
        return success(outboundListService.remove(queryWrapper));
    }
}
